<?php
session_start();
require_once("config.php");
//Will need to check if file is owned by owner if the file is public.

$sel = mysql_query("SELECT * FROM filebin_files WHERE filecode = '" . $loc->db_cmp($_GET['filecode']) . "'");
$res = mysql_fetch_array($sel);

if($res['id']){
	
	if(!$loc->db_cmp($_GET['type'])){
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
		header("Cache-Control: no-store, no-cache, must-revalidate"); 
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache");
		//header("Content-length: " . $thisSize);
		header("Content-Type: " . $res['type']);
		$typeArr = explode("/" , $res['type']);
		
		$imgDat = @getimagesize(UPLOAD_DIR . $res['filecode']);
		
		if($typeArr[0] == "image" && $typeArr[1] != "psd" && $typeArr[1] != "eps" && $imgDat != false){
			$dispo = "inline";
		}else{
			$dispo = "attachment";
		}

		$filename = str_replace(" " , "_" , $loc->db_out($res['filename']));
		
		if(is_file(UPLOAD_DIR . $res['filecode'])){
			header("Content-Disposition: ".$dispo."; filename=" . $filename);
			echo file_get_contents(UPLOAD_DIR . $res['filecode']);
		}
		if(is_file(TEMP_THUMBS_DIR . $res['filecode']).".jpg"){
			header("Content-Disposition: inline; filename=" . $filename);
			echo file_get_contents(TEMP_THUMBS_DIR . $res['filecode'].".jpg");
		}

	}

	if($loc->db_cmp($_GET['type']) == "thumb"){
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
		header("Cache-Control: no-store, no-cache, must-revalidate"); 
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache");
		//header("Content-length: " . $thisSize);
		header("Content-Type: " . $res['type']);

		$dispo = "inline";

		$filename = str_replace(" " , "_" , $loc->db_out($res['filename']));
		header("Content-Disposition: ".$dispo."; filename=" . $filename);

		if($loc->db_cmp($_GET['sub_type']) == "desktop"){
			echo file_get_contents(TEMP_THUMBS_DIR . $res['filecode'] . ".jpg");
		}else{
			echo file_get_contents(THUMBS_DIR . $res['filecode'] . ".jpg");
		}
	}



	if($loc->db_cmp($_GET['type']) == "temp_thumb"){
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
		header("Cache-Control: no-store, no-cache, must-revalidate"); 
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache");
		//header("Content-length: " . $thisSize);
		header("Content-Type: " . $res['type']);
		$typeArr = explode("/" , $res['type']);
		$dispo = "inline";

		$filename = str_replace(" " , "_" , $loc->db_out($res['filename']));

		header("Content-Disposition: ".$dispo."; filename=" . $filename);

		echo file_get_contents(TEMP_THUMBS_DIR . $res['filecode'] . ".jpg");
	}
}else{
	echo "No such file";
	exit;
}
?>